package org.ricks.conditon.test;

import org.ricks.conditon.TaskCondition;
import org.ricks.conditon.test.entity.TaskEntity;
import org.ricks.orm.OrmContext;


public class PlayerLevelCondition extends TaskCondition<LevelUpEvent> {
    private final TaskEntity task;

    public PlayerLevelCondition(TaskEntity task) {
        super(task.getTargetValue(), PlayerLevelCondition::onConditionDone, LevelUpEvent.class);
        this.task = task;
    }

    @Override
    protected void handleEvent(LevelUpEvent event) {
        // 更新任务进度：取最大等级值
        int newValue = Math.max(task.getCurrentValue(), event.getNewLevel());
        boolean isCompleted = task.updateProgress(newValue);
        if (isCompleted) {
            System.out.println("Task completed: " + task.getTaskId());
            return;
        }
//        OrmContext.me().update(task);
    }

    private static void onConditionDone(TaskCondition<?> condition) {
        // 最终确认保存（可选冗余校验）
        if (condition instanceof PlayerLevelCondition pc) {
//            OrmContext.me().update(pc.task);
        }
    }


}
